const fs = require("fs");
const FormData = require("form-data");
const {axiosServer} = require("./myAxios");
const reg3 = /\[pic=(.*),isDecrypt=\d]/
// '[file=D:\\Program Files\\微信数据\\WeChat Files\\wxid_kw9roi42fqv222\\FileStorage\\File\\2023-06\\镀锌板1.2mm激光.dwg]'
const reg49 = /\[file=(.*)]/
module.exports = {
    sleep (time) {
        return new Promise(r => {
            setTimeout(r, time)
        })
    },
    createFormData (filePath) {
        return new Promise((resolve, reject) => {
            console.log('创建表单', filePath)
            let localFilePath, matchResult;
            if ((matchResult = filePath.match(reg3))) {
                localFilePath = matchResult[1]
            } else if ((matchResult = filePath.match(reg49))) {
                localFilePath = matchResult[1]
            }
            let localFile = fs.createReadStream(localFilePath);
            const formData = new FormData();
            formData.append('file', localFile);
            formData.append('userId', 2)
            const headers = formData.getHeaders();//获取headers
            //获取form-data长度
            formData.getLength(function (err, length) {
                if (err) {
                    reject(err)
                }
                headers['content-length'] = length;
                resolve(formData)
            })
        })
    }
}